﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FinPlusCommon;
using FinPlusWPFControls;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Xml;


namespace Test_FinPlusWPFControls
{
    using NUnit.Framework;

    class Test_FinPlusWPFControls
    {
        static void Main(string[] args)
        {

            //Console.WriteLine("Press <Enter> to start test.");
            //Console.ReadLine();

            var test1Thread = new Thread(TextFixture1.Tests);
            test1Thread.SetApartmentState(ApartmentState.STA);
            test1Thread.Start();

            //Console.WriteLine("Press <Enter> to stop test.");
            //Console.ReadLine();
        }
    }

    [TestFixture]// basic add and select, selectAsOf, select from
    static class TextFixture1
    {
        public static void Tests()
        {
            
            TextFixture1.Test1();

            TextFixture1.Test2(); 

        }

        //test filter bar with data
        [Test]
        public static void Test1()
        {
            //path
            var path = AppDomain.CurrentDomain.BaseDirectory;
            //loads config into functions
            var testFunctions = Functions.ParseFunctions(path + "Test1.Control.xml", "root/control");
            //load assembly to be tested
            var testAssembly = System.Reflection.Assembly.LoadFile(path + "FinPlusWPFControls.dll");
            //build functions into object
            var testObject = FinPlusFuncFactory.Construct(testFunctions, testAssembly, "FiltertrayUser");

            //Check 1: any exceptions in build and dock
            var testExceptions = FinPlusFuncFactory.Exceptions(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 2: any null objects
            var testNullObjects = FinPlusFuncFactory.NullObjects(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 3: any circular references
            var testCircularReference = FinPlusFuncFactory.CircularRef(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 4: check number data count
            var data = (FinPlusControl)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "DataUser").Object;
            Assert.AreEqual(data.InfoString("Count"), "3");

            //Trigger event
            var filterbarUserName = (FinPlusFilterbar)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "FilterbarUserName").Object;
            filterbarUserName.TestEvent("fred roberts", "OnClick");

            //Trigger event
            var filterbarUserId = (FinPlusFilterbar)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "FilterbarUserId").Object;
            filterbarUserId.TestEvent("nortonj", "OnClick");

            //Check 5: check view count
            var view = (FinPlusFilterbarTray)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "FiltertrayUser").Object;
            Assert.AreEqual(view.InfoString("Count"), "1");

            //Check 6: check view count
            filterbarUserId.TestEvent("nortonj", "OnClick");
            Assert.AreEqual(view.InfoString("Count"), "2");

            //Check 7: check view count
            filterbarUserName.TestEvent("fred roberts", "OnClick");
            Assert.AreEqual(view.InfoString("Count"), "3");

            //Check 8: any funtions fail to dispose
            var testDispose = FinPlusFuncFactory.Dispose(testFunctions);
            Assert.AreEqual(testDispose.Count(), 0);
        }

        //test grid with data
        [Test]
        public static void Test2()
        {
            //path
            var path = AppDomain.CurrentDomain.BaseDirectory;
            //loads config into functions
            var testFunctions = Functions.ParseFunctions(path + "Test2.Control.xml", "root/control");
            //load assembly to be tested
            var testAssembly = System.Reflection.Assembly.LoadFile(path + "FinPlusWPFControls.dll");
            //build functions into object
            var testObject = FinPlusFuncFactory.Construct(testFunctions, testAssembly, "FiltertrayUser");

            //Check 1: any exceptions in build and dock
            var testExceptions = FinPlusFuncFactory.Exceptions(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 2: any null objects
            var testNullObjects = FinPlusFuncFactory.NullObjects(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 3: any circular references
            var testCircularReference = FinPlusFuncFactory.CircularRef(testFunctions);
            Assert.AreEqual(testExceptions.Count(), 0);

            //Check 4: check data count equals num rows in grid
            var data = (FinPlusControl)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "DataUser").Object;
            var grid = (FinPlusGroupGrid)FinPlusFuncFactory.Construct(testFunctions, testAssembly, "GridUser").Object;
            Assert.AreEqual(grid.InfoString("RowCount"), data.InfoString("Count"));

            //Check 5: any funtions fail to dispose
            Assert.AreEqual(grid.InfoString("ColCount"), "5");

            //Check 6: any funtions fail to dispose
            var testDispose = FinPlusFuncFactory.Dispose(testFunctions);
            Assert.AreEqual(testDispose.Count(), 0);
        }
   
    }
}
